﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using SMS.ServiceManager;
using digit.Core;
using SMS.UserManager;

namespace SMS.Admin.Modules
{
    public partial class ListSportTeam : WebControlBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!ClientState.IsAuthenticated)
            {
                Response.Redirect("~/Login.aspx?session=timeout");
            }

            this.lnkDelete.Attributes.Add("onclick", "return confirm('Bạn đã chắc chắn xóa?');");
            this.lnkDeleteBottom.Attributes.Add("onclick", "return confirm('Bạn đã chắc chắn xóa?');");

            if (ClientState.MemberInfo.UserType != 0 && ClientState.MemberInfo.UserType != 1)
            {
                lnkAdd.Enabled = false;
                lnkAddBottom.Enabled = false;
                lnkDelete.Enabled = false;
                lnkDeleteBottom.Enabled = false;
            }
            else
            {
                lnkAdd.Enabled = true;
                lnkAddBottom.Enabled = true;
                lnkDelete.Enabled = true;
                lnkDeleteBottom.Enabled = true;
            }

            if (IsPostBack) return;

            ViewState["SortExpression"] = "TeamID";
            ViewState["SortDirection"] = SortDirection.Descending;

            this.BindData();
        }

        protected void lnkAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect("Default.aspx?module=DetailSportTeam");
        }

        protected void lnkDelete_Click(object sender, EventArgs e)
        {
            int iDeleted = 0;

            //Search all checked rows to delete
            foreach (GridViewRow r in this.grvTeam.Rows)
            {
                //If checkbox is not checked then quit
                HtmlInputCheckBox cbx = (HtmlInputCheckBox)r.Cells[0].Controls[1];
                if (!cbx.Checked) continue;

                //If checkbox is checked then delete
                if (SportTeamManager.Delete(Util.ToInteger(cbx.Value)))
                    iDeleted++;
            }

            //Display result of delete process
            if (iDeleted == 0)
            {
                this.lblError.Text = "Không có bản ghi nào được xóa!";
                return;
            }

            this.lblError.Text = "Đã xóa " + iDeleted.ToString() + " bản ghi!";

            this.BindData();
        }

        protected void grvTeam_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.grvTeam.PageIndex = e.NewPageIndex;
            this.BindData();
        }

        protected void grvTeam_Sorting(object sender, GridViewSortEventArgs e)
        {
            if (ViewState["SortExpression"].ToString() == e.SortExpression)
            {
                ViewState["SortDirection"] = ((SortDirection)ViewState["SortDirection"] == SortDirection.Ascending ? SortDirection.Descending : SortDirection.Ascending);
            }
            else
            {
                ViewState["SortExpression"] = e.SortExpression;
                ViewState["SortDirection"] = SortDirection.Ascending;
            }
            BindData();
        }

        private void BindData()
        {
            try
            {
                DataTable dt = SportTeamManager.SelectAll();

                if (dt.Rows.Count > 0)
                {
                    DataView dv = dt.DefaultView;

                    dv.Sort = Util.GetSortClause(ViewState["SortExpression"].ToString(), (SortDirection)ViewState["SortDirection"]);
                    if (ViewState["SortExpression"].ToString() != "TeamID")
                    {
                        dv.Sort += ", " + Util.GetSortClause("TeamID", SortDirection.Ascending);
                    }

                    this.grvTeam.DataSource = dv;
                    this.grvTeam.DataBind();
                }
                else
                {
                    lblError.Text = "Hiện tại chưa có đội nào mới được cập nhật";
                }
            }
            catch { }
        }
    }
}
